package com.webcash.CNBoard.Repository;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.webcash.CNBoard.Model.History;
import com.webcash.CNBoard.Model.HistoryWithJobs;

@Repository
public class HistoryRepositoryImpl extends SqlSessionDaoSupport implements HistoryRepository {

	public void addHistoryByProjectNoAndWhereNoAndJobNo(int projectNo,
			int whereNo, int jobNo) {
		SqlSession session = getSqlSession();
		System.out.println(projectNo+"/" + whereNo + "/"+jobNo);
		Map params = new HashMap();
		params.put("projectNo", projectNo);
		params.put("whereNo", whereNo);
		params.put("jobNo", jobNo);
		params.put("writeDate", new Date());
		params.put("modifyDate", new Date());
		
		session.insert("History.addHistoryByProjectNoAndWhereNoAndJobNo", params);
	}


	public List<HistoryWithJobs> selectHistoryListByProjectNo(int projectNo) {
		SqlSession session = getSqlSession();
		
		
		List<HistoryWithJobs> historyWork = session.selectList("History.selectHistoryListForWorkByProjectNo", projectNo);
		List<HistoryWithJobs> historyFile = session.selectList("History.selectHistoryListForFileByProjectNo", projectNo);
		List<HistoryWithJobs> historyDocument = session.selectList("History.selectHistoryListForDocumentByProjectNo", projectNo);
		
		
		List<HistoryWithJobs> historyWithJobs = new ArrayList<HistoryWithJobs>();
		historyWithJobs.addAll(historyWork);
		historyWithJobs.addAll(historyFile);
		historyWithJobs.addAll(historyDocument);
		Collections.sort(historyWithJobs);
		System.out.println(historyWithJobs);
		return historyWithJobs;
	}


	public void deleteHistoryByWhereNoAndJobNo(int whereNo, int jobNo) {
		
		SqlSession session = getSqlSession();
		
		Map params = new HashMap();
		
		params.put("whereNo", whereNo);
		params.put("jobNo", jobNo);
		
		History history = session.selectOne("History.selectHistoryByWhereNoAndJobNo", params);
		
		if(history != null)
			session.delete("History.deleteHistoryByWhereNoAndJobNo", params);
	}
	
	
	
}
